package app

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jassue/jassue-gin/app/common/request"
	"github.com/jassue/jassue-gin/app/common/response"
	"github.com/jassue/jassue-gin/app/services"
)

// 添加角色
func AddRole(c *gin.Context) {
	var form request.AddRole
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	fmt.Printf("%+v\n", &form) //入库

	if err := services.RoleService.AddRole(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}
}

// 编辑角色
func EditRole(c *gin.Context) {
	var form request.EditRole
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	fmt.Printf("%+v\n", &form) //入库

	if err := services.RoleService.EditRole(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 角色列表
func GetRoleLIst(c *gin.Context) {
	var form request.GetRoleList
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	fmt.Printf("%+v\n", &form) //

	if err, result := services.RoleService.GetRoleList(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, result)
	}
}

// 删除角色
func DelRole(c *gin.Context) {
	var form request.RoleId
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	fmt.Printf("%+v\n", &form) //

	if err := services.RoleService.DelRole(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}
}

// 获取组内用户
func GetRoleUsers(c *gin.Context) {
	var form request.RoleId
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	fmt.Printf("%+v\n", &form) //

	if err, result := services.RoleService.GetRoleUsers(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, result)
	}

}

// 获取所有用户
func GetAllUsers(c *gin.Context) {

	if err, result := services.RoleService.GetAllUsers(); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, result)
	}
}

// 组内保存用户 添加用户或删除用户
func SaveRoleUsers(c *gin.Context) {
	//role_id id_source id_result
	var form request.SaveRoleUser
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	//AddRole
	if err := services.RoleService.SaveRoleUsers(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

	fmt.Printf("%+v\n", &form) //

}
